import { Component, OnInit } from '@angular/core';
import { myValidNull } from '@core/util/string-utils';
import { UserEditComponent } from '../user-edit/user-edit.component';
import { NzMessageService } from 'ng-zorro-antd';
import { MyModalHelper } from '@core/my-modal.helper';
import { CurrUserService } from '@core/routes-services/Observer-service/curr-user.service';
import { CurrUserModel } from '@core/routes-services/Observer-service/curr-user.model';

@Component({
  selector: 'app-user-list',
  templateUrl: './user-list.component.html',
})
export class UserListComponent implements OnInit {


  isLoading = false;
  keyword: any;
  page: any = {current_page: 1, per_page: 10, total: 0, data: []};

  constructor(private message: NzMessageService,
                private currUserService:CurrUserService,
                private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }

  initPage() {
    // this.isLoading = true;
    // this.bannerService.pagination(this.page,this.keyword).then((resp)=>{
    //   console.log(resp)
    //   if(resp.status=="success"){
    //     this.page = resp.data;
    //   }else {
    //     this.message.warning(resp.message);
    //   }
    //   this.isLoading = false;
    // },error=>{
    //   console.log(error)
    //   this.isLoading = false;
    // })
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = {current_page: 1, per_page: 10, total: 0, data: [{}]};
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = {current_page: 1, per_page: 10, total: 0, data: [{}]};
    this.page.page = pageNo;
    this.initPage();
  }

  editEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(UserEditComponent, {entity: bak}, 'lg', {
      nzTitle: '编辑用户图信息'
    }).subscribe(u => {
      console.log(u);
      temp ? this.search() : this.changePageNo(this.page.current_page);
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  }

  delete = (data) => {
    // this.bannerService.deleteObj(data.id).then((data) => {
    //   this.search();
    //   this.message.success('删除成功');
    // }, error => console.log('删除出错，后续处理'));
  }

  /**
   * 测试方法
   */
  testObserver(){
    this.currUserService.setCurrUser(new CurrUserModel(1,'哈哈','新角色'))
  }
}
